package com.wang.blog.service.impl;


import com.wang.blog.dao.*;
import com.wang.blog.model.*;
import com.wang.blog.ov.ArticleVo;
import com.wang.blog.ov.PageVo;
import com.wang.blog.service.ArticleService;
import com.wang.blog.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.Query;
import java.time.LocalDateTime;
import java.util.*;


@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private MeArticleDAO meArticleDAO;

    @Autowired
    private SysUserDAO sysUserDAO;

    @Autowired
    private MeArticleBodyDAO meArticleBodyDAO;

    @Autowired
    private MeCategoryDAO meCategoryDAO;

    @Autowired
    private MeTagDAO meTagDAO;

    @Autowired
    private MeArticleTagDAO meArticleTagDAO;
    @Override
    public List<MeArticle> listArticles(PageVo page) {

     MeArticleExample meArticleExample=new MeArticleExample();
     MeArticleExample.Criteria criteria=meArticleExample.createCriteria();


        if (null != page.getName() && !"".equals(page.getName())) {
            meArticleExample.setOrderByClause(page.getName()+" "+page.getSort());
        }

        meArticleExample.setOffset(page.getPageNumber().longValue());
        meArticleExample.setLimit(page.getPageSize());
     return meArticleDAO.selectByExample(meArticleExample);
    }

    @Override
    public List<MeArticle> listArticles(ArticleVo article, PageVo page) {
        return meArticleDAO.listArticlsByArticOVandPageOv(article,page);
    }




    @Override
    public List<MeArticle> findAll() {
        return null;
    }

    @Override
    public MeArticle getArticleById(Integer id) {
        MeArticle meArticle= meArticleDAO.selectByPrimaryKey(id);
        meArticle.setMeArticleBody(meArticleBodyDAO.selectByPrimaryKey(meArticle.getBodyId()));
        meArticle.setSysUser(sysUserDAO.selectByPrimaryKey(meArticle.getAuthorId()));
        meArticle.setMeCategory(meCategoryDAO.selectByPrimaryKey(meArticle.getCategoryId()));

        MeArticleTagExample meArticleTagExample=new MeArticleTagExample();
        MeArticleTagExample.Criteria criteria=meArticleTagExample.createCriteria();

        criteria.andArticleIdEqualTo(meArticle.getId());
        List<MeArticleTag> meArticleTagList= meArticleTagDAO.selectByExample(meArticleTagExample);

        if(meArticleTagList.size()>0) {
         List<MeTag> meTagList=new ArrayList<>();
            for (MeArticleTag x : meArticleTagList
                    ) {
                    meTagList.add(meTagDAO.selectByPrimaryKey(x.getTagId()));
            }
            meArticle.setMeTags(meTagList);
        }
        return meArticle;


    }

    @Override
    public Integer publishArticle(MeArticle article) {
        if(article.getId()!=null){
            article.getMeArticleBody().setId(this.getArticleById(article.getId()).getBodyId());
            meArticleBodyDAO.updateByPrimaryKeySelective(article.getMeArticleBody());

            article.setCategoryId(article.getMeCategory().getId());
            meArticleDAO.updateByPrimaryKeySelective(article);



            MeArticleTagExample meArticleTagExample=new MeArticleTagExample();
            MeArticleTagExample.Criteria criteria=meArticleTagExample.createCriteria();
            criteria.andArticleIdEqualTo(article.getId());
            meArticleTagDAO.deleteByExample(meArticleTagExample);


            MeArticleTag meeArticleTag=new MeArticleTag();
            meeArticleTag.setArticleId(article.getId());
            for (MeTag x:article.getMeTags()
                    ) {

                meeArticleTag.setTagId(x.getId());
                meArticleTagDAO.insertSelective(meeArticleTag);
            }


            return  article.getId();
        }
        int body=meArticleBodyDAO.insert(article.getMeArticleBody());

        article.setWeight(0);
        article.setCommentCounts(0);
        article.setCreateDate(new Date());
        article.setViewCounts(0);
        article.setAuthorId(UserUtils.getCurrentUser().getId());

        article.setBodyId(new Long(article.getMeArticleBody().getId()));
        article.setCategoryId(article.getMeCategory().getId());

        meArticleDAO.insert(article);

        MeArticleTag meArticleTag=new MeArticleTag();
        meArticleTag.setArticleId(article.getId());
        for (MeTag x:article.getMeTags()
             ) {

        meArticleTag.setTagId(x.getId());
        meArticleTagDAO.insertSelective(meArticleTag);
        }
        return article.getId();
    }

    @Override
    public Integer saveArticle(MeArticle article) {
        return null;
    }

    @Override
    public Integer updateArticle(MeArticle article) {
        return meArticleDAO.updateByExampleSelective(article,null);
    }

    @Override
    public void deleteArticleById(Integer id) {
        meArticleDAO.deleteByPrimaryKey(id);
    }

    @Override
    public List<MeArticle> listArticlesByTag(Integer id) {
        return null;
    }

    @Override
    public List<MeArticle> listArticlesByCategory(Integer id) {
        MeArticleExample meArticleExample=new MeArticleExample();
        meArticleExample.createCriteria().andCategoryIdEqualTo(id);

        List<MeArticle> meArticleList= meArticleDAO.selectByExample(meArticleExample);

        for(MeArticle i:meArticleList)
        {i.setMeArticleBody(meArticleBodyDAO.selectByPrimaryKey(i.getBodyId()));
        i.setSysUser(sysUserDAO.selectByPrimaryKey(i.getAuthorId()));
        i.setMeCategory(meCategoryDAO.selectByPrimaryKey(i.getCategoryId()));

        MeArticleTagExample meArticleTagExample=new MeArticleTagExample();
        MeArticleTagExample.Criteria criteria=meArticleTagExample.createCriteria();

        criteria.andArticleIdEqualTo(i.getId());
        List<MeArticleTag> meArticleTagList= meArticleTagDAO.selectByExample(meArticleTagExample);

        if(meArticleTagList.size()>0) {
            List<MeTag> meTagList=new ArrayList<>();
            for (MeArticleTag x : meArticleTagList
                    ) {
                meTagList.add(meTagDAO.selectByPrimaryKey(x.getTagId()));
            }
            i.setMeTags(meTagList);
        }
        }
        return meArticleList;
    }

    @Override
    public MeArticle getArticleAndAddViews(Integer id) {
        MeArticle meArticle=new MeArticle();
        meArticle.setId(id);
        meArticle.setViewCounts(meArticleDAO.selectByPrimaryKey(id).getViewCounts()+1);

         meArticleDAO.updateByPrimaryKeySelective(meArticle);
         return this.getArticleById(id);
    }

    @Override
    public List<MeArticle> listHotArticles(int limit) {
        return  meArticleDAO.listHotArticlsBylimit(limit);
    }

    @Override
    public List<MeArticle> listNewArticles(int limit) {
        return meArticleDAO.listNewArtuclsBylimit(limit);
    }

    @Override
    public List<ArticleVo> listArchives() {
        return meArticleDAO.listArticlsTime();
    }


}
